﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using mshtml;
using SHDocVw;
using System.Text.RegularExpressions;

namespace MeTalker.Aide
{
    internal static class LibHelper
    {
        // Methods
        internal static string GetHtmlURL(IntPtr hwnd)
        {
            if (hwnd == IntPtr.Zero)
            {
                return null;
            }
            string str = "";
            try
            {
                IHTMLDocument2 document;
                IHTMLWindow2 window;
                DocWebBrowse(hwnd, out window, out document);
                if (document != null)
                {
                    str = document.url.Trim();
                }
            }
            catch (Exception exception)
            {
            }
            return str;
        }

        private static string a(string html)
        {
            if ((html == null) || (html.Length == 0))
            {
                return "";
            }
            html = html.Substring(html.IndexOf("-->") + 3);
            string pattern = "<IMG alt=.*?/:.*&nbsp;.*?>";
            string input = Regex.Replace(html, pattern, new MatchEvaluator(CallMatch));
            string str3 = "<[^<]*?>";
            input = Regex.Replace(Regex.Replace(input, str3, "").Replace("&nbsp;", " "), @"(\r\n){2,}", "\r\n").Replace("left!#$!@#$", "<").Replace("right!#$!@#$", ">");
            if (input == null)
            {
                input = "";
            }
            return input;
        }

        private static string CallMatch(Match A_0)
        {
            string str = A_0.ToString();
            int index = str.IndexOf("/:");
            int num2 = str.IndexOf("&nbsp");
            return str.Substring(index, num2 - index).Replace("<", "left!#$!@#$").Replace(">", "right!#$!@#$");
        }

        private static void DocWebBrowse(IntPtr hwnd0, out IHTMLWindow2 htmlWin1, out IHTMLDocument2 htmlWin2)
        {
            htmlWin1 = null;
            htmlWin2 = null;
            Guid gUID = typeof(IWebBrowser2).GUID;
            Guid guid2 = typeof(IA1).GUID;
            Guid guid = typeof(IHTMLDocument2).GUID;
            Guid guid3 = typeof(IHTMLWindow2).GUID;
            Guid guid4 = typeof(IWebBrowserApp).GUID;
            try
            {
                LoadLibrary("OLEACC.DLL");
                int num = RegisterWindowMessage("WM_HTML_GETOBJECT");
                UIntPtr zero = UIntPtr.Zero;
                SendMessageTimeout(hwnd0, num, new IntPtr(), new IntPtr(), 2, 0x3e8, out zero);
                if (zero.ToUInt32() != 0)
                {
                    ObjectFromLresult((int)zero.ToUInt32(), ref guid, 0, out htmlWin2);
                }
            }
            catch (Exception exception)
            {
            }
        }
        internal static string GetHtmlText(IntPtr hwnd)
        {
            string str;
            GetMessageWinHtml(hwnd, out str);
            return str;
        }

        internal static string GetMessageWinHtml(IntPtr hwnd, out string text)
        {
            string innerHTML = "";
            text = string.Empty;
            if (hwnd != IntPtr.Zero)
            {
                try
                {
                    IHTMLDocument2 document;
                    IHTMLWindow2 window;
                    DocWebBrowse(hwnd, out window, out document);
                    if ((document != null) && (document.body != null))
                    {
                        text = document.body.innerText;
                        innerHTML = document.body.innerHTML;
                    }
                }
                catch (Exception ex)
                {
                    if (UtilHelper.DEBUG)
                        MuchInfo.Logs.MILog.Debug("GetMessageWinHtml", ex);
                }
            }
            if (innerHTML == null)
            {
                innerHTML = "";
            }
            return innerHTML;
        }

        internal static void SetMessageHtmlDocument(IntPtr hwnd, out IHTMLDocument2 document)
        {
            document = null;
            if (hwnd != IntPtr.Zero)
            {
                try
                {
                    IHTMLWindow2 window;
                    DocWebBrowse(hwnd, out window, out document);
                }
                catch (Exception ex)
                {
                    if (UtilHelper.DEBUG)
                        MuchInfo.Logs.MILog.Debug("SetMessageHtmlDocument", ex);
                }
            }
        }
        internal static void GetMessage(IntPtr hwnd , out string text, out string html)
         {
            html  = GetMessageWinHtml(hwnd, out text);
        }

        internal static string GetMessageText(IntPtr hwnd)
        {
            string text = "";
            GetMessageWinHtml(hwnd, out text);
            return text;
        }

        internal static string GetMessageWinHtml(IntPtr hwnd)
        {
            string text = "";
            return GetMessageWinHtml(hwnd, out text);
        }

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindowEx(IntPtr A_0, IntPtr A_1, string A_2, string A_3);
        [DllImport("kernel32.dll")]
        public static extern int GetProcAddress(int A_0, string A_1);
        [DllImport("kernel32.dll")]
        public static extern int LoadLibrary(string A_0);
        [return: MarshalAs(UnmanagedType.Interface)]
        [DllImport("oleacc.dll", PreserveSig = false)]
        public static extern object ObjectFromLresult(UIntPtr A_0, [MarshalAs(UnmanagedType.LPStruct)] Guid A_1, IntPtr A_2);
        [DllImport("oleacc.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int ObjectFromLresult(int A_0, ref Guid A_1, int A_2, out IHTMLDocument2 A_3);
        [DllImport("OLEACC.DLL")]
        public static extern int ObjectFromLresult(int A_0, ref Guid A_1, int A_2, [In, Out, MarshalAs(UnmanagedType.Interface)] ref object A_3);
        [DllImport("user32.dll")]
        public static extern int RegisterWindowMessage(string msg);
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr A_0, int A_1, int A_2, ref int A_3);
        [DllImport("user32.dll")]
        public static extern int SendMessageTimeout(int A_0, int A_1, int A_2, int A_3, int A_4, int A_5, ref int A_6);
        [DllImport("User32.dll")]
        public static extern int SendMessageTimeout(IntPtr A_0, int A_1, IntPtr A_2, IntPtr A_3, int A_4, int A_5, out UIntPtr A_6);

        // Nested Types
        [ComImport, Guid("6d5140c1-7436-11ce-8034-00aa006009fa"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IA1
        {
            void a111111(ref Guid A_0, ref Guid A_1, [MarshalAs(UnmanagedType.Interface)] out object A_2);
        }
    }
}